package com.lsk.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jfinal.kit.PropKit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;

public class CacheUtil {
    private static JedisPool jedisPool = null;
    static  {
        //设置配置信息
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMaxTotal(30);
        //初始化
        jedisPool = new JedisPool(jedisPoolConfig, PropKit.get("redis.host"), 6379, 30, PropKit.get("redis.password"));
    }


    //获取redis操作对象
    public static Jedis getJedis() {
        return jedisPool.getResource();
    }

    public static void set(String key, String value) {
        set(key, value, 60 * 60 * 8);
    }

    public static void set(String key, Object value, int expire) {
        Jedis jedis = getJedis();
        jedis.set(key, SerializeUtil.serialize(value));
        jedis.expire(key, expire);
    }

    public static void main(String[] args) {
        //先创建一个Jedis对象
        JedisShardInfo shardInfo = new JedisShardInfo("115.29.161.165", 6379);
        shardInfo.setPassword("liqXAp3ZKMJOdb9K");
        Jedis jedis = new Jedis(shardInfo);
        //调用jedis对象的方法，方法名称和redis命令一致
        jedis.set("key1", "hello");
        String string = jedis.get("key1");
        System.out.println(string);
        //关闭Jedis对象
        jedis.close();
    }
}
